/**
 * Created by fujing on 14-12-23.
 */
/**
 * 全局函数
 */

var Q         = Q || {};
Q.USER_DOMAIN = (function ()
{
    var _host   = window.location.host.split('.');
    var _com    = _host.pop();
    var _domain = _host.pop();
    var _prefix = _host.pop();

    return (_host.length > 0 ? (_host.join('.') + '.') : '') + 'user.' + _domain + '.' + _com;
})();

Q.ST_SERVER = function ()
{
    var _host   = window.location.host.split('.');
    var _com    = _host.pop();
    var _domain = _host.pop();
    var host    = 'st.' + _domain + '.' + _com + '/user';
    return host;
}();

Q.GLOBAL = {};

/**
 * 获取用户信息,并可调用回调函数处理用户信息
 * @param failed
 * @param success
 */
Q.getUserInfo = function (success, failed)
{
    $.get('http://' + Q.USER_DOMAIN, {c : "Login", a : "Sso"}, function (data)
    {
        if (!data || !data.status) {
            failed && failed();
            return false;
        }
        Q.GLOBAL = data.data;
        success && success(data.data);
        return true;
    }, 'jsonp');
};

Q.topUserInit = function (con)
{
    var userDomain   = 'http://' + Q.USER_DOMAIN + '/';
    var userStServer = 'http://' + Q.ST_SERVER + '/';
    var refUrl       = escape(window.location.href);

    Q.getUserInfo(function (data)
    {
        var tpl =
                '<div class="login_suc">' +
                '<span class="f_right cura ml10 mt10"><a class="buzz-out blue" href="' + userDomain + '?c=Default" target="_self">' + data.username + '</a><span> | </span>' +
                '<a class="buzz-out" href="' + userDomain + '?c=login&a=logout&callback=' + refUrl + '" target="_self">退出</a></span>' +
                '<div class="user_photo"><div class="user_img"><img id="img3" src="' + userStServer + 'images/photo.jpg"/></div></div>' + '</div>';
        $(con).html(tpl);
    }, function ()
    {
        var tpl =
                '<div class="login_dz"><image class="loginbg" src="' + userStServer + 'images/loginicon.png"/>' +
                '<a class="buzz-out" href="' + userDomain + '?c=Login&callback=' + refUrl + '" target="_self">登录</a><span> | </span>' +
                '<a class="buzz-out" href="' + userDomain + '?c=Register&callback=' + refUrl + '" target="_self">注册</a>' + '</div>';
        $(con).html(tpl);
    });
};

$(function ()
{
    Q.topUserInit('#login');
});
